function [C, row_num, P] = Generate_Data_Perm(n, d, delta, p, q)


P = zeros(n*d,d);
row_num = n * ones(n,1);
for i = 1:n
    a = randperm(d);
    Pi = zeros(d);
    for j = 1:d
        Pi(j,a(j)) = 1;
    end
    P((i-1)*d+1:i*d,:) = Pi;
end
C = P * P';
for i = 1:n-1
    for j = (i+1):n
        if rand() > p
            row_num(i) = row_num(i) - 1;
            row_num(j) = row_num(j) - 1;
            C((i-1)*d+1:i*d,(j-1)*d+1:j*d) = 0;
            C((j-1)*d+1:j*d,(i-1)*d+1:i*d) = 0;
        else
            if rand() > q
                rand_perm_vec = randperm(d);
                rand_perm_mat = zeros(d);
                for k = 1:d
                    rand_perm_mat(k,rand_perm_vec(k)) = 1;
                end
                C((i-1)*d+1:i*d,(j-1)*d+1:j*d) = rand_perm_mat;
                C((j-1)*d+1:j*d,(i-1)*d+1:i*d) = rand_perm_mat';
            end
            if delta > 0
                Cij = C((i-1)*d+1:i*d,(j-1)*d+1:j*d) + delta * randn(d);
                [assignment, ~] = assignmentoptimal(-Cij);
                A = zeros(d);
                A((1:d)'+(assignment-1)*d) = 1;
                Cij = A;
                C((i-1)*d+1:i*d,(j-1)*d+1:j*d) = Cij;
                C((j-1)*d+1:j*d,(i-1)*d+1:i*d) = Cij';
            end
        end
    end
end